﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SchoolManagementSystem.Controls;
using SchoolManagementSystem.Forms.BusinessForm;
using CoreData;
using SchoolManagementSystem.Details;
using Helper;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class Stock_VoucherOutForm : DetailForm
    {
        long ClassID;
        string TypeOfOut;
        public List<long> VoucherSeletedProductID { get; set; }
        //public long ProviderID { get; set; }
        public long VoucherOutID { get; set; }
        public Stock_VoucherOutForm()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable();
            dateEdit_create.Text = DateTime.Now.ToShortDateString();
            comboboxUC_PaymentMethod.Model = RegisterService.RegisterServices.PaymentMethodService;
            comboboxUC_PaymentMethod.Uc = new Payment_MethodUC();
            comboboxUC_PaymentMethod.ColumnMain = "Name";
            comboboxUC_PaymentMethod.ColumnDisplay = "Code,Name";

            //comboboxUC_StockOutputMethod.Model = RegisterService.RegisterServices.StockMethodService;
            //comboboxUC_StockOutputMethod.Uc = new StockMethodUC(1);
            //comboboxUC_StockOutputMethod.ColumnMain = "Name";
            //comboboxUC_StockOutputMethod.ColumnDisplay = "Code,Name";
            //comboboxUC_Stock.SelectedValue = "2";

            comboboxUC_Stock.Model = RegisterService.RegisterServices.ProductWareHouseService;
            comboboxUC_Stock.Uc = new StockUC();
            comboboxUC_Stock.ColumnMain = "Name";
            comboboxUC_Stock.ColumnDisplay = "Code,Name";
            comboboxUC_Stock.SelectedValue = "1";

            combobox_Staff.Model = RegisterService.RegisterServices.StaffService;
            combobox_Staff.Uc = new StaffUC();
            combobox_Staff.ColumnMain = "Name";
            combobox_Staff.ColumnDisplay = "ID,Name";
            combobox_Staff.SelectedValue = ValueDefine.StaffID.ToString();
            combobox_Staff.Enabled = false;

            comboboxUC_RecivedBy.Model = RegisterService.RegisterServices.StaffService;
            comboboxUC_RecivedBy.Uc = new StaffUC();
            comboboxUC_RecivedBy.ColumnMain = "Name";
            comboboxUC_RecivedBy.ColumnDisplay = "ID,Name";
            comboboxUC_RecivedBy.SelectedValue = ValueDefine.StaffID.ToString();
            comboboxUC_RecivedBy.SelectedValue = "1";

            comboboxUC_Teacher.Model = RegisterService.RegisterServices.TeacherService;
            comboboxUC_Teacher.Uc = new TeacherUC();
            comboboxUC_Teacher.ColumnMain = "Name";
            comboboxUC_Teacher.ColumnDisplay = "Code,Name";

            lookUpEdit_StockInOutMethod.Properties.DataSource = RegisterService.RegisterServices.StockMethodService.GetListByType("OUT");
            lookUpEdit_StockInOutMethod.Properties.ValueMember = "ID";
            lookUpEdit_StockInOutMethod.Properties.DisplayMember = "Name";
            lookUpEdit_StockInOutMethod.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Đơn vị tính"));


            lookUpEdit_Room.Properties.DataSource = RegisterService.RegisterServices.ProductWareHouseService.GetListByParent(" where ParentID = 1");
            lookUpEdit_Room.Properties.ValueMember = "ID";
            lookUpEdit_Room.Properties.DisplayMember = "Name";
            lookUpEdit_Room.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Phòng"));

            lookUpEdit_Place.Properties.DataSource = RegisterService.RegisterServices.ProductWareHouseService.GetListByParent(" where ParentID = 2");
            lookUpEdit_Place.Properties.ValueMember = "ID";
            lookUpEdit_Place.Properties.DisplayMember = "Name";
            lookUpEdit_Place.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Nơi cấp"));

            //comboboxUC_Customer.Model = RegisterService.RegisterServices.CustomerService;
            //comboboxUC_Customer.Uc = new CustomerUC();
            //comboboxUC_Customer.ColumnMain = "Name";
            //comboboxUC_Customer.ColumnDisplay = "Code,Name";
           
            //Khoi tao gia tri cho control
            comboBoxEdit_VAT.SelectedIndex = 0;
            if (checkCodeAuto.Checked == true)
                txtcode.Enabled = false;
            {
                txtcode.Text = RegisterService.RegisterServices.StockVoucherOutService.GetVoucherNo("PXK");
            }

            panelControl_Class.Visible = false;
            panelControl_Customer.Visible = false;

        }
        public DialogResult ShowDialog(long VoucherID)
        {
            Stock_VoucherOut objVoucher = new Stock_VoucherOut();
            if (VoucherID > 0)
            {
                cmdaddproduct.Enabled = false;
                VoucherOutID = VoucherID;
                objVoucher = (Stock_VoucherOut)RegisterService.RegisterServices.StockVoucherOutService.GetByID(VoucherID);
                btnPrintVoucherInPropose.Enabled = false;
            }
            txtcode.Text = objVoucher.VoucherOutNo;
            txtbillno.Text = objVoucher.BillNo;
            dateEdit_create.DateTime = objVoucher.VoucherDate;
            //combobox_Provider.ID = objVoucher.WarehouseID;
            combobox_Staff.ID = objVoucher.EmployeeID;
            comboboxUC_Stock.ID = objVoucher.WarehouseID;
            comboboxUC_PaymentMethod.ID = objVoucher.Payment_MethodID;
            lookUpEdit_StockInOutMethod.EditValue = objVoucher.Output_MethodID;
            txtnote.Text = objVoucher.Note;
            txtSumPrice.Text = objVoucher.Amount.ToString();

            if (objVoucher.Output_MethodID == 2)//Xuat ban hoc vien
            {
                comboboxUC_Teacher.ID = objVoucher.PersonID;
                buttonEdit_Class.EditValue = objVoucher.PlaceID;
            }
            else if (objVoucher.Output_MethodID == 3)//Xuat cap phong hoc
            {
                lookUpEdit_Room.EditValue = objVoucher.PlaceID;
                
            }
            else if (objVoucher.Output_MethodID == 4)//Xuat ban kh
            {
                txtCustomer.Text = objVoucher.CustomerName;
            }
            else// xuat cap noi bo
            {
                lookUpEdit_Place.EditValue = objVoucher.PlaceID;
                comboboxUC_RecivedBy.ID = objVoucher.PersonID;
            }

            if (objVoucher.VAT == false)
            {
                comboBoxEdit_VAT.SelectedIndex = 0;
                txtVATAmount.Text = "0";
            }
            else
            {
                comboBoxEdit_VAT.SelectedIndex = 1;
                txtVATAmount.Text = (objVoucher.Amount * 10 / 100).ToString();
            }
            txtTotalAmount.Text = objVoucher.TotalAmount.ToString();

            //Load Detail
            IList<Stock_VoucherOutDetail> listdetail = RegisterService.RegisterServices.StockVoucherOutDetailService.GetListVODByPID(VoucherID);
            int newRowHandle;
            if (listdetail != null || listdetail.Count > 0)
            {
                IList<Product_Unit> listUnit = (IList<Product_Unit>)RegisterService.RegisterServices.ProductUnitService.GetList();
                if (listUnit.Count > 0)
                {
                    //repositoryItemLookUpEdit_Unit = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
                    repositoryItemLookUpEdit_Unit.DataSource = listUnit;
                }
                repositoryItemLookUpEdit_Unit.ValueMember = "ID";
                repositoryItemLookUpEdit_Unit.DisplayMember = "Name";
                repositoryItemLookUpEdit_Unit.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Đơn vị tính"));
                foreach (var ilist in listdetail)
                {
                    gridView1.AddNewRow();
                    newRowHandle = gridView1.FocusedRowHandle;
                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductNameExt"], ilist.ProductNameExt);
                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductID"], ilist.ID);
                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductCodeExt"], ilist.ProductCodeExt);
                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["Quantity"], ilist.Quantity);
                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["Price"], ilist.Price);
                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["Amount"], ilist.Price * ilist.Quantity);
                    //lay danh sach don vi tinh

                    gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["UnitNameExt"], ilist.UnitID);
                    gridView1.UpdateCurrentRow();
                }
                gridView1.RefreshData();
                UpdateTotal();
                gridView1.OptionsBehavior.Editable = false;
            }

            // end load detail
            this.ShowDialog();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return this.DialogResult;
        }


        private void cmdaddproduct_Click(object sender, EventArgs e)
        {

            frmSelectProduct pro = new frmSelectProduct();

            VoucherSeletedProductID = new List<long>();
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                VoucherSeletedProductID.Add(Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID")));
            }
            //ProviderID = combobox_Provider.ID;
            if (pro.ShowDialog(VoucherSeletedProductID) == DialogResult.OK)
            {
                IList<Product> listproduct = new List<Product>();
                listproduct = RegisterService.RegisterServices.ProductService.GetProductInListID(pro.ProductSeletedIDs);
                int newRowHandle;
                if (listproduct != null)
                {
                    IList<Product_Unit> listUnit = (IList<Product_Unit>)RegisterService.RegisterServices.ProductUnitService.GetList();
                    if (listUnit.Count > 0)
                    {
                        //repositoryItemLookUpEdit_Unit = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
                        repositoryItemLookUpEdit_Unit.DataSource = listUnit;
                    }
                    repositoryItemLookUpEdit_Unit.ValueMember = "ID";
                    repositoryItemLookUpEdit_Unit.DisplayMember = "Name";
                    repositoryItemLookUpEdit_Unit.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Đơn vị tính"));
                    foreach (var iproduct in listproduct)
                    {
                        gridView1.AddNewRow();
                        newRowHandle = gridView1.FocusedRowHandle;
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductNameExt"], iproduct.Name);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductID"], iproduct.ID);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductCodeExt"], iproduct.Code);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["Quantity"], 1);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["Price"], iproduct.Output_Price);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["InStockQuantityExt"], iproduct.InStockQuantityExt);
                        //
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["Amount"], iproduct.Output_Price);
                        //lay danh sach don vi tinh
                        //IList<Product_UnitChange> listUnit = (IList<Product_UnitChange>)RegisterService.RegisterServices.ProductUnitChangeService.GetUnitChangeByProductID(iproduct.ID);

                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["UnitNameExt"], iproduct.Output_UnitID);
                        gridView1.UpdateCurrentRow();
                    }
                    gridView1.RefreshData();
                    UpdateTotal();
                    SaveAble = true;
                    gridView1.Focus();
                }
            }
        }

        void UpdateTotal()
        {
            decimal iteamPrice = 0;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                iteamPrice += Convert.ToDecimal(gridView1.GetRowCellValue(i, "Amount"));
            }
            txtSumPrice.Text = iteamPrice.ToString();
            decimal vat = 0;
            if (comboBoxEdit_VAT.Text == "10%")
                vat = iteamPrice * 10 / 100;
            txtTotalAmount.Text = (iteamPrice + vat).ToString();
        }
        private DataTable CreateTable()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("ProductNameExt", typeof(string));
            tbl.Columns.Add("ProductID", typeof(int));
            tbl.Columns.Add("ProductCodeExt", typeof(string));
            tbl.Columns.Add("Quantity", typeof(int));
            tbl.Columns.Add("Price", typeof(decimal));
            tbl.Columns.Add("UnitNameExt", typeof(int));
            tbl.Columns.Add("Amount", typeof(decimal));
            tbl.Columns.Add("InStockQuantityExt", typeof(decimal));
            return tbl;
        }
        /// <summary>
        /// Get TotalAmount
        /// </summary>
        /// <param name="view">gridview</param>
        /// <param name="listSourceRowIndex">row index</param>
        /// <returns>decimal total amount</returns>
        decimal getTotalValue(DevExpress.XtraGrid.Views.Grid.GridView view, int listSourceRowIndex)
        {
            if (view.Columns["Price"] != null || view.Columns["Quantity"] != null)
            {
                decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Price"));
                decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity"));
                return unitPrice * quantity;
            }
            else
                return 0;
        }
        //int chk = 0;
        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gridView1.IsEditing)
            {
                if (e.Column.FieldName == "Quantity" || e.Column.FieldName == "Price")
                {
                    long Quantity = Convert.ToInt64(gridView1.GetListSourceRowCellValue(e.RowHandle, "Quantity"));
                    long InStockQuantity = Convert.ToInt64(gridView1.GetListSourceRowCellValue(e.RowHandle, "InStockQuantityExt"));
                    if (Quantity <= InStockQuantity)
                    {
                        gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["Amount"], getTotalValue(gridView1, e.RowHandle));
                        gridView1.UpdateCurrentRow();
                        gridView1.RefreshData();
                        UpdateTotal();
                    }
                    else
                    {
                        MessageBox.Show("Số lượng trong kho không đủ", "Cảnh báo tồn", MessageBoxButtons.OK);
                    }
                    //điều chỉnh tiền thuế nếu có
                    decimal vat = 0;
                    decimal iteamPrice = Convert.ToDecimal(txtSumPrice.Text);
                    if (comboBoxEdit_VAT.Text == "10%")
                        vat = Convert.ToDecimal(txtSumPrice.Text) * 10 / 100;
                    txtTotalAmount.Text = (iteamPrice + vat).ToString();
                    txtVATAmount.Text = vat.ToString();
                }
                
                //long _ProductID = Convert.ToInt64(gridView1.GetListSourceRowCellValue(e.RowHandle, "ProductID"));
                //IList<Product_UnitChange> listUnit = (IList<Product_UnitChange>)RegisterService.RegisterServices.ProductUnitChangeService.GetUnitChangeByProductID(_ProductID);
                //long _UnitChangeID = Convert.ToInt64(gridView1.GetListSourceRowCellValue(e.RowHandle, "UnitNameExt"));
                //if ((listUnit != null || listUnit.Count > 0) && listUnit[0].UnitID != _UnitChangeID && chk == 0)
                //{
                   
                //    if (e.Column.FieldName == "UnitNameExt")
                //    {
                       
                //        decimal _price = RegisterService.RegisterServices.ProductService.GetPriceByUnit(_UnitChangeID, _ProductID);
                //        gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["Price"], _price);
                //        UpdateTotal();
                //        gridView1.UpdateCurrentRow();
                //        gridView1.RefreshData();
                //    }
                //}
                //else if (chk == 0 && listUnit[0].UnitID!=_UnitChangeID)
                //{
                //    chk += 1;
                //    string acceptstr = "";
                //    foreach (var item in listUnit)
                //    {
                //        acceptstr += "- "+item.NameExt+"\n";
                //    }
                //    string dispText = gridView1.GetRowCellDisplayText(e.RowHandle, "UnitNameExt");
                //    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UnitNameExt"], listUnit[0].UnitID);
                //    MessageBox.Show("Đơn vị tính: \"" +  dispText+ "\" chưa quy đổi\nCác đơn vị tính hợp lệ:\n " + acceptstr, "Không hợp lệ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //}
            }
            //chk = 0;

        }

        void LoadData()
        {
            if (VoucherOutID != 0)
            {
                var data = RegisterService.RegisterServices.StockVoucherOutService.GetByID(VoucherOutID);

            }
        }

        private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.Column.FieldName == "Amount" && e.IsGetData) e.Value = getTotalValue(view, e.ListSourceRowIndex);
        }


        private void comboBoxEdit_VAT_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtSumPrice.Text != "")
            {
                decimal vat = 0;
                decimal iteamPrice = Convert.ToDecimal(txtSumPrice.Text);
                if (comboBoxEdit_VAT.Text == "10%")
                    vat = Convert.ToDecimal(txtSumPrice.Text) * 10 / 100;
                txtTotalAmount.Text = (iteamPrice + vat).ToString();
                txtVATAmount.Text = vat.ToString();
            }
        }
        private string ValidateVoucher()
        {
            string msg="";
            if (lookUpEdit_StockInOutMethod.EditValue == null)
            {
                msg += "Chưa chọn phương thức xuất\n";
            }
            else
            {
                //VoucherOut.provider
                if (TypeOfOut == "XBLH")
                {
                    if (comboboxUC_Teacher.ID <= 0)
                        msg += "Chưa chọn giáo viên\n";
                    if (ClassID == 0)
                        msg += "Chưa chọn lớp\n";
                }
                else if (TypeOfOut == "XBKH")
                {
                    if (string.IsNullOrEmpty(txtCustomer.Text))
                        msg += "Nhập tên người mua\n";
                }
                else if (TypeOfOut == "XCPH")
                {
                    if (lookUpEdit_Room.EditValue == null)
                        msg += "Chưa chọn Phòng học\n";
                }
                else if (TypeOfOut == "XCNB")
                {
                    if (comboboxUC_RecivedBy.ID <= 0)
                        msg += "Chưa chọn người nhận\n";
                    if (lookUpEdit_Place.EditValue == null)
                        msg += "Chưa chọn nơi cấp\n";
                }
                else
                {

                }
            }
            return msg;
        }
        protected override void Print()
        {
            //frmReportDetailForm frm = new frmReportDetailForm();
            //frm.ShowDialog(VoucherOutID, "VoucherOutForm");
        }
        protected override long Save()
        {
            string validateString = ValidateVoucher();
            if (validateString != string.Empty)
            {
                MessageBox.Show(validateString, "Nhập liệu", MessageBoxButtons.OK);
                return -1;
            }
            else
            {
                Stock_VoucherOut VoucherOut = new Stock_VoucherOut();
                if (ID > 0)
                {
                    VoucherOut = (Stock_VoucherOut)RegisterService.RegisterServices.StockVoucherOutService.GetByID(ID);
                    VoucherOut.LastModifiedBy = ValueDefine.StaffID;
                    VoucherOut.LastModifiedDate = DateTime.Now;
                }
                else
                {
                    VoucherOut.CreatedBy = ValueDefine.StaffID;
                    VoucherOut.CreatedDate = DateTime.Now;
                    VoucherOut.LastModifiedBy = ValueDefine.StaffID;
                    VoucherOut.LastModifiedDate = DateTime.Now;
                }
                VoucherOut.VoucherOutNo = txtcode.Text;
                VoucherOut.BillNo = txtbillno.Text;
                VoucherOut.VoucherDate =dateEdit_create.DateTime.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute).AddSeconds(DateTime.Now.Second);// DateTime.Now;
                //VoucherOut.provider
                if (TypeOfOut == "XBLH")
                {
                    VoucherOut.PersonID = comboboxUC_Teacher.ID;
                    VoucherOut.PlaceID = ClassID;//(long)buttonEdit_Class.EditValue;
                }
                else if (TypeOfOut == "XBKH")
                {
                    VoucherOut.CustomerName = txtCustomer.Text;
                    VoucherOut.PlaceID = 0;
                }
                else if (TypeOfOut == "XCPH")
                {
                    VoucherOut.PlaceID = (long)lookUpEdit_Room.EditValue;
                    VoucherOut.PersonID = 0;
                }
                else if(TypeOfOut == "XCNB")
                {
                    VoucherOut.PersonID = comboboxUC_RecivedBy.ID;
                    VoucherOut.PlaceID = (long)lookUpEdit_Place.EditValue;
                }
                else if(TypeOfOut == "XCHV")
                {
                    VoucherOut.PersonID = (long)buttonEdit_Student.EditValue;
                    //VoucherOut.PlaceID = (long)lookUpEdit_Place.EditValue;
                }
                VoucherOut.TypeOfOut = TypeOfOut;
                VoucherOut.CreatedDate = dateEdit_create.DateTime;
                VoucherOut.Output_MethodID = (long)lookUpEdit_StockInOutMethod.EditValue;
                VoucherOut.Note = txtnote.Text;
                VoucherOut.WarehouseID = comboboxUC_Stock.ID;
                VoucherOut.Payment_MethodID = comboboxUC_PaymentMethod.ID;
                VoucherOut.EmployeeID = combobox_Staff.ID;
                VoucherOut.Amount = Convert.ToDecimal(txtSumPrice.Text);
                VoucherOut.Active = true;

                if (txtVATAmount.Text == "0")
                    VoucherOut.VAT = false;
                else
                    VoucherOut.VAT = true;
                VoucherOut.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
                ID = RegisterService.RegisterServices.StockVoucherOutService.Save(VoucherOut);

                Stock_VoucherOutDetail voucherDetail;
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    long a = Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID"));
                    voucherDetail = new Stock_VoucherOutDetail();
                    voucherDetail.ProductID = Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID"));
                    voucherDetail.Quantity = Convert.ToInt32(gridView1.GetRowCellValue(i, "Quantity"));
                    voucherDetail.UnitID = Convert.ToInt64(gridView1.GetRowCellValue(i, "UnitNameExt"));
                    voucherDetail.Price = Convert.ToDecimal(gridView1.GetRowCellValue(i, "Price"));
                    voucherDetail.Amount = Convert.ToDecimal(gridView1.GetRowCellValue(i, "Amount"));
                    voucherDetail.Stock_VoucherOutID = ID;
                    RegisterService.RegisterServices.StockVoucherOutDetailService.Save(voucherDetail);
                }
                VoucherOutID = ID;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return ID;
        }
        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (gridView1.SelectedRowsCount > 0)
                {
                    if (MessageBox.Show("Bạn có chắc muốn xóa dữ liệu đã chọn trong danh sách?", "Xóa dữ liệu", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                    { gridView1.DeleteSelectedRows(); }
                }
                else
                {
                    MessageBox.Show("Chọn dữ liệu cần xóa trong danh sách", "Xóa dữ liệu", MessageBoxButtons.OK);
                }
            }

        }
       
        private void buttonEdit_Class_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectClassFilter selectclass = new frmSelectClassFilter();
                if (selectclass.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + selectclass.Code + "] " + selectclass.Name;
                    buttonEdit_Class.EditValue = data;
                    ClassID = selectclass.ID;
                }
                else
                {
                    buttonEdit_Class.EditValue = "";
                    ClassID = 0;
                }
            }
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (lookUpEdit_StockInOutMethod.EditValue.ToString() == "2")//Xuất bán lớp
            {
                panelControl_Class.Visible = true;
                panelControl_Customer.Visible = false;
                panelControl_TranferRoom.Visible = false;
                panelControl_Salon.Visible = false;
                panelControl_Student.Visible = false;
                TypeOfOut = "XBLH";
            }
            else if (lookUpEdit_StockInOutMethod.EditValue.ToString() == "4")//Xuất bán khách hàng
            {
                panelControl_Customer.Visible = true;
                panelControl_Class.Visible = false;
                panelControl_TranferRoom.Visible = false;
                panelControl_Salon.Visible = false;
                panelControl_Student.Visible = false;
                TypeOfOut = "XBKH";
            }
            else if(lookUpEdit_StockInOutMethod.EditValue.ToString() == "3")// Xuất cấp phòng học
            {
                panelControl_TranferRoom.Visible = true;
                panelControl_Salon.Visible = false;
                panelControl_Class.Visible = false;
                panelControl_Customer.Visible = false;
                panelControl_Student.Visible = false;
                TypeOfOut = "XCPH";
            }
            else if (lookUpEdit_StockInOutMethod.EditValue.ToString() == "5")//Xuất cấp nội bộ
            {
                panelControl_TranferRoom.Visible = false;
                panelControl_Salon.Visible = true;
                panelControl_Class.Visible = false;
                panelControl_Customer.Visible = false;
                panelControl_Student.Visible = false;
                TypeOfOut = "XCNB";
            }
            else if (lookUpEdit_StockInOutMethod.EditValue.ToString() == "6")//Xuất bán le học viên
            {
                panelControl_TranferRoom.Visible = false;
                panelControl_Salon.Visible = false;
                panelControl_Class.Visible = false;
                panelControl_Customer.Visible = false;
                panelControl_Student.Visible = true;
                TypeOfOut = "XBHV";
            }
        }
        long StudentID;
        private void buttonEdit_Student_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                frmSelectStudent selectFrm = new frmSelectStudent();
                if (selectFrm.ShowDialog() == DialogResult.OK)
                {
                    string data = "[" + selectFrm.Code + "] " + selectFrm.Name;
                    buttonEdit_Student.EditValue = data;
                    StudentID = selectFrm.ID;
                }
                else
                {
                    buttonEdit_Student.EditValue = "";
                    StudentID = 0;
                }
            }
        }

        private void btnPrintvoucherOutPropose_Click(object sender, EventArgs e)
        {
            Stock_VoucherOut voucherOutPropose = new Stock_VoucherOut();
            voucherOutPropose.VoucherOutNo = txtcode.Text;
            voucherOutPropose.BillNo = txtbillno.Text;
            voucherOutPropose.VoucherDate = dateEdit_create.DateTime;
            //voucherOut.provider
            voucherOutPropose.CreatedDate = dateEdit_create.DateTime;
            //voucherOutPropose.Input_MethodID = (long)lookUpEdit_StockInOutMethod.EditValue;
            //voucherOutPropose.Note = txtnote.Text;
            voucherOutPropose.WarehouseID = comboboxUC_Stock.ID;
            voucherOutPropose.Payment_MethodID = comboboxUC_PaymentMethod.ID;
            voucherOutPropose.EmployeeID = combobox_Staff.ID;
            voucherOutPropose.Amount = Convert.ToDecimal(txtSumPrice.Text);
            voucherOutPropose.Active = true;

            List<Stock_VoucherOutDetail> listvoucherOutDetail = new List<Stock_VoucherOutDetail>();
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                Stock_VoucherOutDetail voucherDetail = new Stock_VoucherOutDetail();
                voucherDetail.ProductCodeExt = Convert.ToString(gridView1.GetRowCellValue(i, "ProductCodeExt"));
                voucherDetail.ProductNameExt = Convert.ToString(gridView1.GetRowCellValue(i, "ProductNameExt"));
                voucherDetail.Quantity = Convert.ToInt32(gridView1.GetRowCellValue(i, "Quantity"));
                voucherDetail.UnitNameExt = repositoryItemLookUpEdit_Unit.GetDisplayText(gridView1.GetRowCellValue(i, "UnitNameExt"));//Convert.ToString(gridView1.GetRowCellValue(i, "UnitNameExt"));
                voucherDetail.Price = Convert.ToDecimal(gridView1.GetRowCellValue(i, "Price"));
                voucherDetail.Amount = Convert.ToDecimal(gridView1.GetRowCellValue(i, "Amount"));
                listvoucherOutDetail.Add(voucherDetail);

            }
            frmReportDetailForm frm = new frmReportDetailForm();
            frm.ShowDialog(voucherOutPropose, listvoucherOutDetail);
        }

        private void checkCodeAuto_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCodeAuto.Checked == true)
                txtcode.Enabled = false;
            else
            { txtcode.Enabled = true; txtcode.Text = RegisterService.RegisterServices.StockVoucherOutService.GetVoucherNo("PXK"); }
        }


    }


}